package com.test.demo;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * ${DESCRIPTION}
 *
 * @author muxiaoyu
 * @create 2017/05/24 9:03
 **/
public class StreamDemo {
    public static void main(String[] args) {
        FileOutputStream fileOutputStream = null;
        File file = new File("C:\\Users\\Administrator\\Desktop\\HelloWorld.txt");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (Exception e) {
                throw new RuntimeException("路径无效！");
            }
        }
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write("hello world".getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void test001() throws IOException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\你好.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        fileOutputStream.write("你好".getBytes());
        fileOutputStream.close();

        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] bytes = new byte[1024];
        StringBuffer sb = new StringBuffer();
        int len = 0;
        System.out.println((len = 998));
        while ((len = fileInputStream.read(bytes)) != -1) {
            sb.append(new String(bytes, 0, len, "utf-8"));
        }
        System.out.println(sb.toString());

    }

    @Test
    public void test998() {
        int i = 9;
        System.out.println(i = 10);
    }
}
